package com.system.blogAdmin.projectmanage.utils;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.storage.persistent.FileRecorder;
import com.qiniu.util.Auth;
import com.system.blogAdmin.utils.Properties;
import org.apache.commons.io.FilenameUtils;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.file.Paths;

/**
 * 作者： cnc
 * 创建时间：2017-09-26.
 * 版本：1.0
 */
public class UploadFiledUtils {


    /**
     * 获取文件名的名称 ，去掉后缀名
     * @param fileName 文件名全称
     * @return 去掉后缀名后的文件名的名称
     */
    public static String getBaseName (String fileName){return FilenameUtils.getBaseName(fileName);}

    /**
     * 获取文件后缀名
     * @param fileName 文件名全称
     * @return 后缀名
     */
    public static String getSuffix (String fileName) { return  FilenameUtils.getExtension(fileName);}


    public static String uploadFile(File srcFile){
        return "";
    }

    public static String downLoadFile(String fileName,String domainOfBucket) throws UnsupportedEncodingException {

        String encodedFileName = URLEncoder.encode(fileName, "utf-8");
        String publicUrl = String.format("%s/%s", Properties.qn_download_url, encodedFileName);
        String accessKey = Properties.accessKey;
        String secretKey = Properties.secreKey;
        Auth auth = Auth.create(accessKey, secretKey);
        long expireInSeconds = 3600;//1小时，可以自定义链接过期时间
        String finalUrl = auth.privateDownloadUrl(publicUrl, expireInSeconds);
        System.out.println(finalUrl);
        return  finalUrl;
    }




    public static void FiledUpload(){
        //构造一个带指定Zone对象的配置类
        Configuration cfg = new Configuration(Zone.zone2());
        //...生成上传凭证，然后准备上传
        String accessKey = Properties.accessKey;
        String secretKey = Properties.secreKey;
        String bucket = Properties.bucket;
        String localFilePath = "G:\\newFile\\Love.html";
        String key = null;
        Auth auth = Auth.create(accessKey,secretKey);
        String upToken = auth.uploadToken(bucket);


        String localTempDir = Paths.get(System.getenv("java.io.tmpdir"), Properties.bucket).toString();
        try {
            //设置断点续传文件进度保存目录
            FileRecorder fileRecorder = new FileRecorder(localTempDir);
            UploadManager uploadManager = new UploadManager(cfg, fileRecorder);
            try {
                Response response = uploadManager.put(localFilePath, key, upToken);
                //解析上传成功的结果
                DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
                System.out.println(putRet.key);
                System.out.println(putRet.hash);

            } catch (QiniuException ex) {
                Response r = ex.response;
                System.err.println(r.toString());
                try {
                    System.err.println(r.bodyString());
                } catch (QiniuException ex2) {
                    //ignore
                }
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

    public static void main(String[] args) {

//        OSSFileClient ossFileClient = new OSSFileClient();
//        ossFileClient.uploadFile();
    }

}
